{extend name="/base"}

{block name="header"}
    <h2>分配权限</h2>
{/block}

{block name="body"}
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="" class="layui-form-label">角色名称</label>
            <div class="layui-word-aux layui-form-mid">{$role->name}</div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">角色描述</label>
            <div class="layui-word-aux layui-form-mid">{$role->description}</div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">权限</label>
            <div class="layui-input-block">
                {foreach $permissions as $p1}
                <dl class="cate-box">
                    <dt>
                        <div class="cate-first"><input id="menu{$p1['id']}" type="checkbox" name="permissions[]" value="{$p1['id']}" title="{$p1['name']}" lay-skin="primary" {if isset($p1['own']) && $p1['own'] } checked {/if} ></div>
                    </dt>
                    {if isset($p1['child']) && !empty($p1['child']) }
                    {foreach $p1['child'] as $p2 }
                    <dd>
                        <div class="cate-second"><input id="menu{$p1['id']}-{$p2['id']}" type="checkbox" name="permissions[]" value="{$p2['id']}" title="{$p2['name']}" lay-skin="primary" {if isset($p2['own']) && $p2['own'] } checked {/if} ></div>
                        {if isset($p2['child']) && !empty($p2['child']) }
                        <div class="cate-third">
                            {foreach $p2['child'] as $p3 }
                            <input type="checkbox" id="menu{$p1['id']}-{$p2['id']}-{$p3['id']}" name="permissions[]" value="{$p3['id']}" title="{$p3['name']}" lay-skin="primary" {if isset($p3['own']) && $p3['own'] } checked {/if} >
                            {/foreach}
                        </div>
                        {/if}
                    </dd>
                    {/foreach}
                    {/if}
                </dl>
                {/foreach}
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="formDemo">确 认</button>
                <a  class="layui-btn layui-btn-sm" href="{:url('index')}" >返 回</a>
            </div>
        </div>
    </form>
{/block}

{block name="js"}
<script>
    layui.use(['jquery','table','element','form','layer'],function () {
        var table=layui.table;
        var $=layui.jquery;
        var element=layui.element;
        var form=layui.form;
        var layer=layui.layer;

        form.on('submit(formDemo)',function (data) {
            var load = layer.load();
            $.post("{:url('permission',['id'=>$role->id])}",data.field,function (res) {
                layer.close(load);
                if (res.code==1){
                    layer.msg(res.msg,{icon:1,time:1500},function () {
                        if (res.url) location.href = res.url;
                    })
                } else {
                    layer.msg(res.msg,{icon:2,time:1500})
                }
            });
            return false
        })

        form.on('checkbox', function (data) {
            var check = data.elem.checked;//是否选中
            var checkId = data.elem.id;//当前操作的选项框
            if (check) {
                //选中
                var ids = checkId.split("-");
                if (ids.length == 3) {
                    //第三极菜单
                    //第三极菜单选中,则他的上级选中
                    $("#" + (ids[0] + '-' + ids[1])).prop("checked", true);
                    $("#" + (ids[0])).prop("checked", true);
                } else if (ids.length == 2) {
                    //第二季菜单
                    $("#" + (ids[0])).prop("checked", true);
                    $("input[id*=" + ids[0] + '-' + ids[1] + "]").each(function (i, ele) {
                        $(ele).prop("checked", true);
                    });
                } else {
                    //第一季菜单不需要做处理
                    $("input[id*=" + ids[0] + "-]").each(function (i, ele) {
                        $(ele).prop("checked", true);
                    });
                }
            } else {
                //取消选中
                var ids = checkId.split("-");
                if (ids.length == 2) {
                    //第二极菜单
                    $("input[id*=" + ids[0] + '-' + ids[1] + "]").each(function (i, ele) {
                        $(ele).prop("checked", false);
                    });
                } else if (ids.length == 1) {
                    $("input[id*=" + ids[0] + "-]").each(function (i, ele) {
                        $(ele).prop("checked", false);
                    });
                }
            }
            form.render();
        });

    })
</script>
{/block}